<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .left_aside {
        width: 132px;
        height: 500px;
    }

    .left_tree {
        margin-top: 10px;
        margin-left: -5px;
    }

    /*当前分类标识颜色*/
    /*.left_tree .is-current .el-tree-node__content{*/
    /*    background-color: #F5F7FA;*/
    /*}*/

    .el-tree-node {
        padding-top: 7px;
    }

    .note {
        width: 100%;
        height: 320px;
    }

    .el-upload input[type=file] {
        display: none;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 128px;
        height: 128px;
        line-height: 128px;
        text-align: center;
    }

    .avatar {
        width: 128px;
        height: 128px;
        display: block;
    }
</style>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="请输入标题"
                      size="medium"></el-input>
        </div>
        <!--搜索条件-->
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.status"
                    filterable
                    placeholder="全部状态"
                    size="medium">
                <el-option
                        v-for="item in options"
                        :key="item.val"
                        :label="item.name"
                        :value="item.val">
                </el-option>
            </el-select>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--新增按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>新增点播视频</el-button>
        </div>
        <!--批量删除按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" type="danger" @click="batchDel"><i class="ti-plus"></i>批量删除</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                @selection-change="handleSelectionChange"
                style="width: 100%">

            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column
                    prop="title"
                    label="标题"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="picUrl"
                    label="标题图"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
                <template scope="scope">
                    <p v-if="scope.row.picUrl">
                        <img :src=scope.row.picUrl style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                    prop="info"
                    label="简介"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="地址"
                    prop="url"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="状态"
                    prop="disabled"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                </template>
            </el-table-column>

            <el-table-column
                    label="播放次数"
                    prop="times"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>

            <el-table-column
                    sortable
                    prop="publishAt"
                    label="发布时间"
                    header-align="center"
                    align="center">
                <template scope="scope">
                    {{formatAt(scope.row.publishAt)}}
                </template>
            </el-table-column>
            <!--操作下拉菜单-->
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="scope.row.disabled"
                                              :command="{type:'disabled', id:scope.row.id, status: 0}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item v-else-if="!scope.row.disabled"
                                              :command="{type:'disabled', id:scope.row.id, status: 1}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit', id:scope.row.id, row:scope.row}">
                                修改
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            :title="titlePopu==0?'新增':'修改'"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="80px">

            <el-form-item prop="title" label="标题">
                <el-input placeholder="请输入标题"
                          v-model="formData.title"
                          auto-complete="off"
                          tabindex="1"
                          type="text"
                          style="width: 215px">
                </el-input>
            </el-form-item>

            <el-form-item prop="picUrl" label="标题图">
                <el-upload
                        class="avatar-uploader"
                        tabindex="5"
                        action="${base}/open/file/upload/image2"
                        name="Filedata"
                        :show-file-list="false"
                        :before-upload="beforeUpload"
                        :on-success="function(resp,file,fileList){return handlePicSuccess(resp,file,fileList,'picUrl')}">
                    <img v-if="formData.picUrl" :src="formData.picUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item prop="info" label="简介">
                <el-input placeholder="请输入简介"
                          v-model="formData.info"
                          auto-complete="off"
                          tabindex="1"
                          type="text">
                </el-input>
            </el-form-item>

            <el-form-item prop="url" label="视频地址">
                <el-upload
                        class="avatar-uploader"
                        tabindex="5"
                        action="${base}/open/file/upload/mp4ToM3u8"
                        name="Filedata"
                        :show-file-list="false"
                        :before-upload="beforeVideoUpload"
                        :on-success="handleVideoSuccess"
                        :on-progress="uploadVideoProcess">
                    <p v-if="formData.url !=''">{{formData.url}}</p>
                    <i v-else-if="formData.url =='' && videoFlag == false"
                       class="el-icon-plus avatar-uploader-icon"></i>
                    <el-progress v-if="videoFlag == true" type="circle" :percentage="videoUploadPercent"
                                 style="margin-top:30px;"></el-progress>
                </el-upload>
            </el-form-item>
        </el-form>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                options: [{
                    val: '',
                    name: '全部状态'
                }, {
                    val: '0',
                    name: '启用'
                }, {
                    val: '1',
                    name: '禁用'
                }],
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                searchDate: [],
                editDialogVisible: false,
                titlePopu: 0,
                tableData: [],
                selectData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    status: "",
                    searchKeyword: "",
                    searchDate: "",
                },
                formData: {
                    id: "",
                    title: "",
                    picUrl: "",
                    info: "",
                    url: ""
                },
                formRules: {
                    title: [
                        {required: true, message: '请输入标题', trigger: 'blur'}
                    ],
                    picUrl: [
                        {required: true, message: '请上传标题图', trigger: 'blur'}
                    ],
                    info: [
                        {required: true, message: '请输入简介', trigger: 'blur'}
                    ],
                    url: [
                        {required: true, message: '请上传视频', trigger: 'blur'}
                    ]
                },
                loading: false,
                fileType: ['jpg', 'png', 'jpeg', 'gif'],
                videoType: ['mp4'],
                videoFlag: false,
                videoUploadPercent: ""
            }
        },
        methods: {
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                let self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/cms/video/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {
                let self = this;
                self.titlePopu = 0;
                self.editDialogVisible = true;
                if (self.$refs["editForm"]) {
                    self.$refs["editForm"].resetFields();
                }
                self.formData = {url: ""};
                self.videoFlag = false;
            },
            dropdownCommand: function (command) {//监听下拉框事件
                let self = this;
                if ("edit" === command.type) {
                    self.editDialogVisible = true;
                    if (this.$refs["editForm"]) {
                        this.$refs["editForm"].resetFields();
                    }
                    //点播回显
                    self.formData = {
                        id: command.row.id,
                        title: command.row.title,
                        picUrl: command.row.picUrl,
                        info: command.row.info,
                        url: command.row.url
                    };
                    self.titlePopu = 1;
                    self.videoFlag = false;
                }
                if ("disabled" === command.type) {
                    $.post(base + "/platform/cms/video/disabled", {
                        id: command.id,
                        status: command.status
                    }, function (data) {
                        if (data.code === 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            self.pageData();
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
            },
            doEdit: function () {
                let self = this;
                self.$refs["editForm"].validate(function (valid) {
                    let url = "";
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu === 0) {
                            url = "/platform/cms/video/add";
                        } else {
                            url = "/platform/cms/video/edit";
                        }
                        $.post(base + url, self.formData, function (data) {
                            if (data.code === 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.doSearch();
                                self.editDialogVisible = false;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            batchDel: function () {
                let self = this;
                if (self.selectData.length === 0) {
                    self.$message({
                        message: "请选择视频记录",
                        type: 'warning'
                    });
                    return false;
                }
                let ids = [];
                self.selectData.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要删除' + ids.length + '条数据？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" === a) {//确认后再执行
                            $.post(base + "/platform/cms/video/del", {ids: ids.toString()}, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            handlePicSuccess: function (response, file, fileList, attrName) {// 上传图片
                if (response.code === 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            },
            beforeUpload: function (file) {
                let isLt200M = file.size / 1024 / 1024 < 200;
                if (this.fileType.indexOf(file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length)) < 0) {
                    this.$message.error('文件格式不符，仅可以上传' + this.fileType.join(','));
                    return false;
                }
                if (!isLt200M) {
                    this.$message.error('文件大小不能超过200MB');
                    return false;
                }
                return true;
            },
            beforeVideoUpload: function (file) {
                let isLt120M = file.size / 1024 / 1024 < 1024;
                if (this.videoType.indexOf(file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length)) < 0) {
                    this.$message.error('文件格式不符，仅可以上传' + this.videoType.join(','));
                    return false;
                }
                if (!isLt120M) {
                    this.$message.error('文件大小不能超过1个G');
                    return false;
                }
                return true;
            },
            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            uploadVideoProcess(event, file, fileList) {
                this.videoFlag = true;
                this.videoUploadPercent = file.percentage.toFixed(0);
            },
            handleVideoSuccess(res, file) {                               //获取上传图片地址
                this.videoFlag = false;
                this.videoUploadPercent = 0;
                if (res.code === 0) {
                    this.formData.url = res.data;
                } else {
                    this.$message.error('视频上传失败，请重新上传！');
                }
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>

<!--#
}
#-->